import * as types from '@/stores/mutation-types'
import Vue from 'vue'
import Vuex from 'vuex'
import qs from 'qs'
import axios from 'axios'
import RcUtil from '@/utils'
// import Module_Common from './modules/common'
// import Module_Login from './modules/login'
// import Module_Menu from './modules/menu'
// import Module_Count from './modules/count'
// import Module_Search from './modules/search'
// import Module_SendManage from './modules/sendManage'
// import Module_PurchaseManage from './modules/purchasemanage'
// import Module_StockManage from './modules/stockManage'

Vue.use(Vuex);

const state = {
    domain: 'http://rap.superboss.cc/mockjsdata/295'
};
const getters = {
    domain(state) {
        return state.domain;
    }
};
const mutations = {
    [types.GLOBAL_DOMAIN](state, domain) {
        state.domain = domain;
    }
};
const actions = {
    ajax_server({
        state,
        commit
    }, options) {
        let requestDomain = state.domain,
            defaultOptions = {
                method: 'get',
                headers: {},
                needLoading: true //是否需要loading, 默认需要
            },
            opts = RcUtil.extend(true, defaultOptions, options);
        if (opts.dataType === 'form') {
            opts.headers['Content-Type'] = 'application/x-www-form-urlencoded';
            opts.data = qs.stringify(opts.data || {});
        }
        if(opts.mock){  //使用mock数据
            requestDomain = 'http://rap.superboss.cc/mockjsdata/295'
        }
        return new Promise((resolve, reject) => {
            opts.needLoading = false;
            if (opts.needLoading) {
                Vue.$loading.open(opts.loadingText || '加载中...');
            }
            try {
                axios({
                    method: opts.method || 'get',
                    url: requestDomain + '' + opts.url,
                    params: opts.params || {},
                    data: opts.data,
                    headers: opts.headers
                }).then(resp => {
                    if (opts.needLoading) {
                        Vue.$loading.close();
                    }
                    let json = resp.data;
                    if (json.result == 1) {
                        resolve(json);
                    // } else if (json.result == 901) {
                    //     Vue.$messagebox.alert({
                    //         content: '会话异常，请重新登录！',
                    //         ok() {
                    //             location.href = "#/login";
                    //             reject(json);
                    //         }
                    //     });
                    // } else if (json.result == 904) {
                    //     commit(types.GLOBAL_DOMAIN, json.message);
                    //     if(RcUtil.isPC()){
                    //         location.href= json.message + '#/menu';
                    //     }
                    //     resolve(json);
                    } else {
                        reject(json);
                    }
                }).catch(resp => {
                    if (opts.needLoading) {
                        Vue.$loading.close();
                    }
                    let json = resp.data || {};
                    reject(json);
                });
            } catch (e) {
                if (opts.needLoading) {
                    Vue.$loading.close();
                }
                reject({});
            }
        });
    }
};

export default new Vuex.Store({
    state,
    getters,
    mutations,
    actions,
    // modules: {
    //     Module_Common,
    //     Module_Login,
    //     Module_Menu,
    //     Module_Count,
    //     Module_Search,
    //     Module_SendManage,
    //     Module_PurchaseManage,
    //     Module_StockManage
    // }
});